package com.walle.ak47.commons.redis;

import org.redisson.Config;
import org.redisson.Redisson;
import org.redisson.RedissonClient;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import com.walle.ak47.commons.utils.ConvertUtils;
import com.walle.ak47.commons.utils.PropertiesReader;
import com.walle.ak47.commons.utils.StringUtils;

public class RedissonServices implements InitializingBean, DisposableBean {

	static RedissonClient redisson ;
	
	@Override
	public void afterPropertiesSet() throws Exception {

		String REDIS_SUBSCRIPTION_CONNECTION_POOL_SIZE = PropertiesReader.getWebConfig("REDIS_SUBSCRIPTION_CONNECTION_POOL_SIZE") ;
		if ( StringUtils.isBlank(REDIS_SUBSCRIPTION_CONNECTION_POOL_SIZE) )
			REDIS_SUBSCRIPTION_CONNECTION_POOL_SIZE = "480" ;
		
		Config config = new Config();
		config.useSingleServer()
				.setAddress(PropertiesReader.getWebConfig("REDIS_HOST")+":"+PropertiesReader.getWebConfig("REDIS_PORT"))
				.setPassword( PropertiesReader.getWebConfig("REDIS_PASSWORD") ) 
				.setConnectionPoolSize( ConvertUtils.toInt(PropertiesReader.getWebConfig("REDIS_CONNECTION_POOL_SIZE")) )
				.setSubscriptionConnectionPoolSize( ConvertUtils.toInt(REDIS_SUBSCRIPTION_CONNECTION_POOL_SIZE) )
				.setConnectTimeout( ConvertUtils.toInt(PropertiesReader.getWebConfig("REDIS_CONNECTION_TIME_OUT")) )
				.setTimeout(  ConvertUtils.toInt(PropertiesReader.getWebConfig("REDIS_TIME_OUT")) ) ;
		redisson = Redisson.create(config);
	}

	public static RedissonClient getRedisson() {
		return redisson;
	}
	
	@Override
	public void destroy() throws Exception {
		redisson.shutdown();
	}

}
